//
//  HRCommonData.m
//  haierapp
//
//  Created by zhang on 14-8-7.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRCommonData.h"

static HRCommonData *commonData=nil;

@implementation HRCommonData

+(HRCommonData *)shareCommonData{
    if (commonData==nil) {
        commonData=[[HRCommonData alloc]init];
        
        commonData.hasNewOrder=YES;//默认，第一次打开加载订单列表
        commonData.hasNewShopCar=YES;//默认，第一次打开加载购物车信息
        commonData.isUserScoreChanged = YES; //默认，第一次打开加载用户积分
    }
    
    return commonData;
}



#pragma mark - User Login And Logout


-(HRUser *)getLoginUserFromDefaults{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSDictionary *userDictionary=[defaults objectForKey:LOGIN_USER_KEY_IN_DEFAULTS];
    
    if (userDictionary!=nil) {
        commonData.curUser=[[HRUser alloc]initWithDictionary:userDictionary error:nil];
    }else{
        commonData.curUser=nil;
    }
    
    return commonData.curUser;
}


-(void)loginUser:(HRUser *)user{
    NSDictionary *userDictionary=[user toDictionary];
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:userDictionary forKey:LOGIN_USER_KEY_IN_DEFAULTS];
    
    commonData.curUser=user;
    
    [self setDataHasNew];
}

-(void)logout{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:nil forKey:LOGIN_USER_KEY_IN_DEFAULTS];
    
    commonData.curUser=nil;

    
    //处理用户退出后的数据
    [self setDataHasNew];
    
    //清除购物车本地缓存数据
    [self clearProductsFromShopCar];
}

//从服务器获取最新数据
-(void)setDataHasNew{
    
    commonData.hasNewOrder=YES;//加载订单数据
    commonData.hasNewShopCar=YES;//加载购物车数据
    commonData.isUserScoreChanged = YES;//加载用户积分
    commonData.orderList=nil;//缓存订单数据
}


#pragma mark - 未登陆购物车操作

//添加购物车
-(void)addProductToShopCar:(HRGoodsInfo *)goodsInfo andProductId:(NSString *)productId{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *productArray=[defaults objectForKey:SHOP_CAR_DEFAULTS];
    
     HRProductInfo *productInfo=[[HRProductInfo alloc]initWithGoodsInfo:goodsInfo];
    NSMutableDictionary *productDic=[NSMutableDictionary dictionaryWithDictionary:[productInfo toDictionary]];
    
    [productDic setValue:productId forKey:@"productId"];
    
    if (productArray==nil) {
        
        NSMutableArray *products=[NSMutableArray arrayWithObjects:productDic, nil];
        
        [defaults setObject:products forKey:SHOP_CAR_DEFAULTS];
        
    }else{
        BOOL isExist=NO;
        
        for (NSDictionary *infoDic in productArray) {
            
            HRProductInfo *info = [[HRProductInfo alloc]initWithDictionary:infoDic error:nil];
            
            if ([info.productId intValue] == [productId intValue]) {
                isExist=YES;
                break;
            }
        }
        
        NSMutableArray *products=[NSMutableArray arrayWithArray:productArray];
        
        if (!isExist) {
            [products addObject:productDic];
            [defaults setObject:products forKey:SHOP_CAR_DEFAULTS];
        }
        
    }
    
}

//从购物车删除
-(void)deleteProductFromShopCar:(NSString *)goodsId{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *productArray=[defaults objectForKey:SHOP_CAR_DEFAULTS];
    
    if (productArray!=nil) {
        
        NSMutableArray *products = [NSMutableArray arrayWithArray:productArray];
        
        for (NSDictionary *infoDic in products) {
            HRProductInfo *info=[[HRProductInfo alloc]initWithDictionary:infoDic error:nil];
            
            if ([goodsId intValue] == [info.productId intValue]) {
                [products removeObject:infoDic];
                
                break;
            }
        }
        
        [defaults setObject:products forKey:SHOP_CAR_DEFAULTS];
    }
}

//清除购物车数据
-(void)clearProductsFromShopCar{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:nil forKey:SHOP_CAR_DEFAULTS];
}

//本地缓存购物车产品的数量
-(int)CountProductInShopCar{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *productArray=[defaults objectForKey:SHOP_CAR_DEFAULTS];
    
    int count=0;
    
    if (productArray!=nil && [productArray count]>0) {
        count=[productArray count];
    }
    
    return count;
}

#pragma mark - 获取UUID
-(NSString *)getDeviceUUID{
    NSString *serviceName=@"com.haier";
    NSString *keyName=@"uuid";
    NSString *uuid;
    
    SSKeychainQuery *query=[[SSKeychainQuery alloc] init];
    query.service=serviceName;
    query.account=keyName;
    
    NSError *error=nil;
    [query fetch:&error];
    
    if ([error code]==errSecItemNotFound) {
        //没有保存该UUID
        uuid=[self createUUID];
        [SSKeychain setPassword:uuid forService:serviceName account:keyName];
    }else if (error==nil){
        uuid=query.password;
    }

    
    return uuid;
}

-(NSString *)createUUID{
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    
    CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
    
    NSString *result = [NSString stringWithFormat:@"%@", uuidStr];
    
    return result;
}

@end
